package com.wdd.studentmanager.service.Impl;

import com.wdd.studentmanager.domain.Clazz;
import com.wdd.studentmanager.mapper.ClazzMapper;
import com.wdd.studentmanager.service.ClazzService;
import com.wdd.studentmanager.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageBean<Clazz> queryPage(Map<String, Object> paramMap) {
        Integer pageno = (Integer) paramMap.get("pageno");
        Integer pagesize = (Integer) paramMap.get("pagesize");
        if (pageno == null || pagesize == null) {
            throw new IllegalArgumentException("Page number and page size cannot be null");
        }

        PageBean<Clazz> pageBean = new PageBean<>(pageno, pagesize);
        paramMap.put("startIndex", pageBean.getStartIndex());

        List<Clazz> datas = clazzMapper.queryList(paramMap);
        pageBean.setDatas(datas);

        Integer totalsize = clazzMapper.queryCount(paramMap);
        pageBean.setTotalsize(totalsize);

        return pageBean;
    }

    @Override
    @Transactional
    public int addClazz(Clazz clazz) {
        if (clazz == null || clazz.getName() == null) {
            throw new IllegalArgumentException("Class object or class name cannot be null");
        }
        return clazzMapper.addClazz(clazz);
    }

    @Override
    @Transactional
    public int deleteClazz(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new IllegalArgumentException("Class ID list cannot be null or empty");
        }
        return clazzMapper.deleteClazz(ids);
    }

    @Override
    @Transactional
    public int editClazz(Clazz clazz) {
        if (clazz == null || clazz.getId() == null) {
            throw new IllegalArgumentException("Class object or class ID cannot be null");
        }
        return clazzMapper.editClazz(clazz);
    }

    @Override
    public Clazz findByName(String clazzName) {
        if (clazzName == null || clazzName.trim().isEmpty()) {
            throw new IllegalArgumentException("Class name cannot be null or empty");
        }
        return clazzMapper.findByName(clazzName);
    }
}
